package com.example.ymm.modules1.service.impl.hl.kq;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.KqDayLog;
import com.example.ymm.modules1.entity.my.user.KqDept;
import com.example.ymm.modules1.mapper.my.user.KqDayLogMapper;
import com.example.ymm.modules1.query.kq.KqDayLogQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.kq.UserDayLogDto;
import com.example.ymm.modules1.service.dto.heng.kq.UserDayLogQueryCriteria;
import com.example.ymm.modules1.service.hl.kq.KqDayLogService;
import com.example.ymm.modules1.service.hl.kq.KqDeptService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@DSTransactional
@DS("slave2")
@Slf4j
public class KqDayLogServiceImpl extends BaseServiceImpl<KqDayLogMapper, KqDayLog> implements KqDayLogService {

    @Autowired
    KqDeptService kqDeptService;

    @Override
    public List<KqDayLog> queryAll(KqDayLogQueryCriteria criteria) {
        List<KqDayLog> result =baseMapper.selectList(QueryHelpPlus.getPredicate(KqDayLog.class, criteria));
        return result;
    }

    @Override
    public List<KqDayLog> getDayLogList(KqDayLogQueryCriteria criteria) {
        return baseMapper.getDayLogList(criteria);
    }


    @Override
    public TableList<UserDayLogDto> userDaysCountTable(UserDayLogQueryCriteria criteria, YmPageDto ymPageDto) {

//        String userName=getUserName();
//        KqDept kqDept = kqDeptService.findByGroupUserName(userName);
//        List<String> groupIdIn=new ArrayList<>();
//        if(UtilValidate.isNotEmpty(kqDept)){
//            //criteria.setGroupUserId(kqDept.getGroupUserId());
//            groupIdIn.add(kqDept.getId());
//        }
//        KqDept kqDept2 = kqDeptService.findByManageUserName(userName);
//        if(UtilValidate.isNotEmpty(kqDept2)){
//            groupIdIn.add(kqDept2.getId());
////            criteria.setManageUserId(kqDept2.getManageUserId());
//        }
//        criteria.setGroupIdIn(groupIdIn);
        getPage(ymPageDto);

        List<UserDayLogDto> arrs = userDaysCount(criteria);
        for (UserDayLogDto userDayLogDto : arrs) {
            userDayLogDto.setMonth(criteria.getMonth());
        }
        PageInfo<UserDayLogDto> pageInfo = new PageInfo<>(arrs);
        TableList<UserDayLogDto> tableList = new TableList(pageInfo);
        return tableList;
    }

    @Override
    public List<UserDayLogDto> userDaysCount(UserDayLogQueryCriteria criteria) {
        return baseMapper.userDaysCount(criteria);
    }

    @Override
    public TableList<KqDayLog> userDayListTable(KqDayLogQueryCriteria criteria, YmPageDto pageDto) {

        IPage<KqDayLog> page = new Page<KqDayLog>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(KqDayLog.class, criteria);
        predicate.orderByAsc("add_date");
        baseMapper.selectPage(page, predicate);
        TableList<KqDayLog> tableList=new TableList(page);
        return tableList;
    }
}
